﻿/**
 * Jean-François Desgagné 2009 Portfolio
 * Copyright (c) 2009 Jean-François Desgagné <jfesgagne@gmail.com>
 * 
 * This work is licensed under a Creative Commons Attribution 3.0 United States License.
 * Some Rights Reserved.
 */
package com.jfdesgagne.jfdesgagne09.shell.view {
	import com.jfdesgagne.jfdesgagne09.common.IPipeAwareModule;
	import com.jfdesgagne.jfdesgagne09.common.PipeAwareModuleConstants;
	import com.jfdesgagne.jfdesgagne09.shell.ApplicationFacade;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.utilities.pipes.interfaces.IPipeMessage;
	import org.puremvc.as3.multicore.utilities.pipes.messages.Message;
	import org.puremvc.as3.multicore.utilities.pipes.plumbing.Junction;
	import org.puremvc.as3.multicore.utilities.pipes.plumbing.JunctionMediator;
	import org.puremvc.as3.multicore.utilities.pipes.plumbing.Pipe;
	import org.puremvc.as3.multicore.utilities.pipes.plumbing.TeeMerge;
	import org.puremvc.as3.multicore.utilities.pipes.plumbing.TeeSplit;
	
	public class ShellJunctionMediator extends JunctionMediator {

		public static const NAME:String = 'ShellJunctionMediator';
		
		public function ShellJunctionMediator() {
			super(NAME, new Junction());
		}

		override public function onRegister():void {}
		
		/**
		 * ShellJunction related Notification list.
		 */
		override public function listNotificationInterests():Array {
 			var $interests:Array = super.listNotificationInterests();			
				$interests.push(PipeAwareModuleConstants.MODULE_ADDED);
			return $interests; 
		}

		override public function handleNotification(note:INotification):void {
			switch(note.getName()) {	
				case PipeAwareModuleConstants.MODULE_ADDED:
					this._connectModule( note.getBody() as IPipeAwareModule );
					break;
				default: // Let super handle the rest (ACCEPT_OUTPUT_PIPE, ACCEPT_INPUT_PIPE, SEND_TO_LOG)
					super.handleNotification(note);
			}	
		}
			
		override public function handlePipeMessage($message:IPipeMessage):void {
			this.sendNotification(Message($message).getType(), Message($message).getBody());
		}
			
		public function _connectModule($module:IPipeAwareModule):void {
			this.junction.registerPipe(PipeAwareModuleConstants.SHELL_MODULE_PIPE, Junction.OUTPUT, new TeeSplit());
			this.junction.registerPipe(PipeAwareModuleConstants.MODULE_SHELL_PIPE, Junction.INPUT, new TeeMerge());
			this.junction.addPipeListener(PipeAwareModuleConstants.MODULE_SHELL_PIPE, this, this.handlePipeMessage);
			
			// module -> shell
			var $moduleToShell:Pipe = new Pipe();
			
			$module.acceptOutputPipe(PipeAwareModuleConstants.MODULE_SHELL_PIPE, $moduleToShell);
			
			var $shellInFitting: TeeMerge = junction.retrievePipe(PipeAwareModuleConstants.MODULE_SHELL_PIPE) as TeeMerge;
				$shellInFitting.connectInput($moduleToShell);
			
			// shell -> module
			var $shellToModule:Pipe = new Pipe();
			$module.acceptInputPipe(PipeAwareModuleConstants.SHELL_MODULE_PIPE, $shellToModule);
			
			var $shellOutFitting: TeeSplit = junction.retrievePipe(PipeAwareModuleConstants.SHELL_MODULE_PIPE) as TeeSplit;
				$shellOutFitting.connect($shellToModule);	
		}					
		
		public function _disconnectModule():void {
			junction.removePipe(PipeAwareModuleConstants.MODULE_SHELL_PIPE);
			junction.removePipe(PipeAwareModuleConstants.SHELL_MODULE_PIPE);	 

		}	
				
	}
}